<?php 
namespace plugins\sms\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\sms\lib\Common as LCOM;
use think\Config;

//山牛短信配置首页
class AliyunSmsController extends PluginAdminBaseController
{
    //配置页
    public function config( $var )
    {
        $config = LCOM::getFwsConfig( "aliyun_sms" );
        $this->assign( 'config' , $config );
        
        return $this->display( $var );
    }
    
    //保存配置
    public function saveConfig()
    {
        $param = $this->request->param()['data'];
        $data = [];
        
        if( !empty( $param [ 'mb_list' ] ) ){
        
            //先处理模板
            $mb = autoHtmlspecialcharsDecode ( $param [ 'mb_list' ] );
            $mb = str_replace( [ "\n" , "\r\n" ] , ';' , $mb );
            
            $mb = explode( ";" , $mb );
            $isok = true;
            
            foreach ( $mb as $key => $value ) {
                $item = explode( '#', $value );
                try {
                    list( $mb_name,$mb_title,$mb_sign,$mb_number,$mb_remark) = $item;
                    $item = [
                        'mb_name' => $mb_name,
                        'mb_title' => $mb_title,
                        'mb_sign' => $mb_sign,
                        'mb_number' => $mb_number,
                        'mb_remark' => $mb_remark,
                    ];
                } catch (\Exception $e ) {
                    
                    $isok = false;
                    break;
                }
                $data [] = $item;
            }
            
            if( !$isok ){
                return api_json_echo( false , '模板配置数据错误,保存失败' );
            }
        }
        $res = LCOM::saveMb( 'aliyun_sms' , $data , false );
 
        
        if( $res ){
            $res [ 'key' ] = cmf_var_isset( $param , 'key' ); 
            $res [ 'key_secret' ] = cmf_var_isset( $param , 'key_secret' ); 
            if( LCOM::setFwsConfig( 'aliyun_sms' , $res ) ){
                return api_json_echo( true , '参数保存成功' );
            }
        }
        return api_json_echo( false , '参数保存失败' );
        
    }
    
    //保存模板
    public function saveMb()
    {
        $data = $this->request->param()[ 'mblist' ];
        $list = explode( '#', $data );
        try {
            list( $mb_name,$mb_title,$mb_sign,$mb_number,$mb_remark) = $list;
        } catch (\Exception $e ) {
            return api_json_echo( false , '模板配置数据错误,保存失败' );
        }
        
        $data = [
            [
                'mb_name' => $mb_name,
                'mb_title' => $mb_title,
                'mb_sign' => $mb_sign,
                'mb_number' => $mb_number,
                'mb_remark' => $mb_remark,
            ]
        ];
        if( LCOM::saveMb( 'aliyun_sms' , $data ) ){
            return api_json_echo ( true , '模板保存成功' );
        }
        return api_json_echo ( false , '模板保存失败' );
    }
    
    //删除模板
    public function deleteMb()
    {
        $param = $this->request->param();
        $key = cmf_var_isset( $param , 'key' , '' );

        if( $key && LCOM::deleteMb( "aliyun_sms" , $key ) ){
            return api_json_echo( true , '删除模板成功' );
        }
        return api_json_echo( false , '删除模板失败' );
    }
    
}